<?php
class ContentsController extends AppController {
	var $name = 'Contents';
	var $components = array('Init', 'Param');
	var $helpers = array('Html');

	function beforeFilter() {
		if (!$this->Session->read('admin')) {
			$this->redirect('/login');
		}
		else {
			$this->Init->startup($this);
			$this->Admin = $this->Init->init();

			// menu
			$this->set('dashboard',0);
			$this->set('content',1);
			$this->set('products',0);
			$this->set('component',0);
			$this->set('system',0);

			$this->set('title','Menus Management');
			$this->set('sub_title', $this->Param->getAction($this->params));
  		}
	}

	function index(){
		$contents = $this->Content->find('all', array(
			'conditions' => array(
				'Content.status' => 1
				),
			'order' 	 => 'Content.date desc'
		));
		$this->set('contents',$contents);
	}

	function edit($id = null) {
		$this->loadModel('Content');
		App::import('Vendor', 'SpawEditor', array('file' => 'editor/spaw.inc.php'));
		$id = $this->Param->getID($this->params);

		if (isset($this->data)) {
			$this->data['Content']['date'] = time();

			if ($this->Content->save($this->data)) {
				$this->Session->setFlash('Done successfully!');
				$this->redirect('/contents');
			}

		}
		else {
			if (isset($id) && ($id > 0)) {
				$this->data = $this->Content->find('first',array(
					'conditions' => array('Content.id' => $id)
				));
			}
		}
	}
}
?>